package com.tougutips.modules.user.info.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tougutips.common.utils.StringUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.tougutips.modules.user.info.constant.UserInfoConstants;
import com.tougutips.modules.user.info.entity.User;
import com.tougutips.modules.user.info.mapper.UserMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IService<User> {

    /**
     * 根据账号查询用户业务
     * @param account
     * @return
     */
    public AjaxResult getByAccount(String account) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account", account);
        User user = this.getOne(wrapper);
        if(StringUtil.isEmpty(user)){
            return AjaxResult.error("没有找到此用户.");
        }
        return AjaxResult.success(user);
    }

    /**
     * 新增用户业务
     * @param account
     * @return
     */
    public AjaxResult save(String account){
        Date date = new Date();
        User user = new User();
        user.setId(null);
        user.setAccount(account);
        user.setNickname(UserInfoConstants.NICKNAME);
        user.setGender(UserInfoConstants.MAN);
        user.setAvatar(UserInfoConstants.AVATAR);
        user.setBirthday(date);
        user.setCreateTime(date);
        user.setUpdateTime(date);
        user.setIsDeleted(UserInfoConstants.DELETE_STATUS);
        // 新增用户操作
        boolean save = this.save(user);
        if(!save){
            return AjaxResult.error("用户新增失败.");
        }
        return AjaxResult.success(user);

    }
}
